<?php
// 数据库连接
try {
    $pdo = new PDO('mysql:host=localhost;dbname=library', 'root', '123456');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("数据库连接失败: " . $e->getMessage());
}

// 获取 book_No
if (!isset($_GET['book_No'])) {
    die("未指定图书编号");
}

$book_No = $_GET['book_No'];

// 查询图书信息
$stmt = $pdo->prepare('
    SELECT b.book_No, b.B_name, b.ISBN, b.Recommendation_degree, p.P_name AS Publisher_name, c.Cate_name, a.author_name
    FROM book b
    LEFT JOIN publisher p ON b.P_ID = p.P_ID
    LEFT JOIN book_cate bc ON b.book_No = bc.book_No
    LEFT JOIN category c ON bc.Cate_No = c.Cate_No
    LEFT JOIN authors a ON b.book_No = a.author_id
    WHERE b.book_No = ?
');
$stmt->execute([$book_No]);
$book = $stmt->fetch(PDO::FETCH_ASSOC);

if (!$book) {
    die("未找到图书信息");
}

// 查询图书评论
$stmt = $pdo->prepare('
    SELECT u_No, Re_score, Re_pre
    FROM bookcomment
    WHERE book_No = ?
');
$stmt->execute([$book_No]);
$comments = $stmt->fetchAll(PDO::FETCH_ASSOC);
?>

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($book['B_name']); ?> - 图书详情</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 0;
            background-color: #f8f8f8;
            color: #333;
        }

        .container {
            width: 80%;
            margin: 20px auto;
            background: #fff;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            border-radius: 5px;
        }

    

        .book-info {
            margin-bottom: 20px;
        }

        .book-info ul {
            list-style: none;
            padding: 0;
        }

        .book-info li {
            margin: 5px 0;
        }

        .highlight {
            color: #ff8800;
            font-weight: bold;
        }

        .rating {
            font-size: 18px;
            color: #ffcc00;
        }

        .comments {
            margin-top: 20px;
        }

        .comment {
            border-top: 1px solid #ddd;
            padding: 10px 0;
        }

        .comment:last-child {
            border-bottom: 1px solid #ddd;
        }

        footer {
            background-color: #333;
            color: #fff;
            text-align: center;
            padding: 10px 0;
            position: fixed;
            width: 100%;
            bottom: 0;
        }

        .description {
            margin: 20px 0;
            background: #f9f9f9;
            padding: 15px;
            border-left: 4px solid #ff8800;
            font-style: italic;
        }

        header {
            background-color: #333;
            color: #fff;
            padding: 10px 20px;
            text-align: center;
        }

        header h1 {
            margin: 0;
        }
    </style>
</head>

<body>

    <header>
        <h1>图书管理系统</h1>
    </header>
    <div class="container">
        <h1><?php echo htmlspecialchars($book['B_name']); ?></h1>
        <p>作者: <span class="highlight"><?php echo htmlspecialchars($book['author_name'] ?? '未知'); ?></span></p>
        <div class="book-info">
            <ul>
                <li>图书编号: <?php echo htmlspecialchars($book['book_No']); ?></li>
                <li>ISBN: <?php echo htmlspecialchars($book['ISBN']); ?></li>
                <li>类别: <?php echo htmlspecialchars($book['Cate_name'] ?? '未分类'); ?></li>
                <li>推荐度: <span class="rating"><?php echo str_repeat('★', $book['Recommendation_degree']); ?></span></li>
                <li>出版社: <?php echo htmlspecialchars($book['Publisher_name'] ?? '未知出版社'); ?></li>
            </ul>
        </div>

        <h3>评论：</h3>
        <div class="comments">
            <?php if ($comments): ?>
                <?php foreach ($comments as $comment): ?>
                    <div class="comment">
                        <p><strong>用户ID:</strong> <?php echo htmlspecialchars($comment['u_No']); ?></p>
                        <p><strong>评分:</strong> <?php echo htmlspecialchars($comment['Re_score']); ?> / 10</p>
                        <p><strong>评论:</strong> <?php echo htmlspecialchars($comment['Re_pre']); ?></p>
                    </div>
                <?php endforeach; ?>
            <?php else: ?>
                <p>暂无评论。</p>
            <?php endif; ?>
        </div>
    </div>

    <footer>
        <p>&copy; 2024 图书管理系统 | 版权所有 HMY,GSM</p>
    </footer>
</body>

</html>